package com.kiteqi.wxorder.web;

import com.kiteqi.wxorder.entity.ProductCategory;
import com.kiteqi.wxorder.entity.ProductInfo;
import com.kiteqi.wxorder.service.CategoryService;
import com.kiteqi.wxorder.service.ProductService;
import com.kiteqi.wxorder.utils.ResultVOUtil;
import com.kiteqi.wxorder.vo.ProductInfoVO;
import com.kiteqi.wxorder.vo.ProductVO;
import com.kiteqi.wxorder.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO list(){

        //1，查询所有的上架商品
        List<ProductInfo> productList = productService.findUpAll();

        //2，查询类目
        List<ProductCategory> categoryList = categoryService.findAll();

        //3，数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory category : categoryList){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(category.getCategoryType());
            productVO.setCategoryName(category.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productList) {
                if (productInfo.getCategoryType().equals(category.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        return ResultVOUtil.success(productVOList);
    }
}
